//package com.base.Filter;
//
//import com.alibaba.fastjson.JSONObject;
//import com.base.Filter.sign.BodyReaderHttpServletRequestWrapper;
//import com.base.Filter.sign.HttpUtils;
//import com.base.Filter.sign.SignUtil;
//import com.base.po.param.NetState;
//import lombok.extern.log4j.Log4j;
//import org.springframework.context.annotation.Configuration;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.PrintWriter;
//import java.util.SortedMap;
//
///**
// * 签名过滤器
// *
// * @author show
// * @date 10:03 2019/5/30
// * @Component 注册 Filter 组件
// */
//@Log4j
//@Configuration
//@WebFilter(urlPatterns = "/callApi/*", filterName = "SignAuthFilter") //暂时不生效，只能通过底下的判断URL手动判断
//public class SignAuthFilter implements Filter {
//    static final String FAVICON = "/favicon.ico";
//
//    @Override
//    public void init(FilterConfig filterConfig) {
//        log.info("初始化 SignAuthFilter");
//    }
//
//    @Override
//    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
//        HttpServletResponse response = (HttpServletResponse) res;
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json; charset=utf-8");
//        // 防止流读取一次后就没有了, 所以需要将流继续写出去
//        HttpServletRequest request = (HttpServletRequest) req;
//        HttpServletRequest requestWrapper = new BodyReaderHttpServletRequestWrapper(request);
//        //除非是获取CallApi里面的JAR 否则就不处理
//        String requestURI = request.getRequestURI();
//        if (!requestURI.contains("callApi")){
//            chain.doFilter(request, response);
//        }else {
//            //获取图标不需要验证签名
//            if (FAVICON.equals(requestWrapper.getRequestURI())) {
//                chain.doFilter(request, response);
//            }else {
//                //获取全部参数(包括URL和body上的)
//                SortedMap<Object, Object> allParams = HttpUtils.getAllParams(requestWrapper);
//                boolean systemSign = SignUtil.verifyParamNessary(allParams);
//                String urltimestamp = allParams.get("timestamp") != null ? allParams.get("timestamp").toString() : "";
//                if (urltimestamp==null || urltimestamp.equals("")){
//                    PrintWriter out = response.getWriter();
//                    JSONObject resParam = new JSONObject();
//                    resParam.put("errorCode", NetState.TIME_OUT_VERIFY);
//                    resParam.put("data", "请传入timestamp");
//                    out.append(resParam.toJSONString());
//                    return;
//                }
//                Long achtime = Long.parseLong(urltimestamp);
//                //时间不能大于10分钟
//                if ((System.currentTimeMillis() - achtime) / (1000 * 60) > 10) {
//                    //校验失败返回前端
//                    PrintWriter out = response.getWriter();
//                    JSONObject resParam = new JSONObject();
//                    resParam.put("errorCode", NetState.TIME_OUT_VERIFY);
//                    resParam.put("data", "请求过时");
//                    out.append(resParam.toJSONString());
//                    return;
//                }
//                if (!systemSign) {
//                    log.info("缺少系统级参数,或者参数为空");
//                    //校验失败返回前端
//                    PrintWriter out = response.getWriter();
//                    JSONObject resParam = new JSONObject();
//                    resParam.put("errorCode", NetState.NEED_SYSTEM_PARAM);
//                    resParam.put("data", "缺少系统级参数,或者必填参数为空");
//                    out.append(resParam.toJSONString());
//                } else {
//                    //对参数进行签名验证
//                    boolean isSigned = SignUtil.verifySign(allParams);
//                    if (isSigned) {
//                        log.info("签名通过,[接口]:"+requestURI+"[参数]"+allParams.toString());
//                        chain.doFilter(requestWrapper, response);
//                    } else {
//                        log.info("参数校验出错");
//                        //校验失败返回前端
//                        PrintWriter out = response.getWriter();
//                        JSONObject resParam = new JSONObject();
//                        resParam.put("errorCode", NetState.PARAM_VERIFY_FILURE);
//                        resParam.put("data", "参数校验出错");
//                        out.append(resParam.toJSONString());
//                    }
//                }
//            }
//        }
//
//    }
//
//    @Override
//    public void destroy() {
//        log.info("销毁 SignAuthFilter");
//    }
//}